package treeDemo;

import java.util.LinkedList;



/**
 * 
 * @author zyj 2018-5-4 09:35:39
 *
 * @function 二叉树层序遍历--从根开始，依次向下，对于每一层从左向右遍历。
 *
 *步骤：  队列，先进先出
 *1、对于不为空的节点，先把该节点加入到队列中
 *
 *2、从队列中拿出节点，如果该节点左右节点都不为空，分别把左右节点加入到队列中
 *
 *3、重复以上操作直到队列为空
 *
 */
public class TreePrint0 {
	

	/**
	 * 打印形状
	 *       6
	 *     5    8
	 *   2  6  7  11
	 *-10 3         15 		
	 * @param args
	 */
	public static void main(String[] args) {
		TreeNode tr1 = new TreeNode(6);
		TreeNode tr2 = new TreeNode(5);
		TreeNode tr3 = new TreeNode(8);
		tr1.left=tr2;
		tr1.right=tr3;
		TreeNode tr4 = new TreeNode(2);
		TreeNode tr5 = new TreeNode(6);
		tr2.left=tr4;
		tr2.right=tr5;
		TreeNode tr6 = new TreeNode(7);
		TreeNode tr7 = new TreeNode(11);
//		tr3.left=tr6;
//		tr3.right=tr7;
		TreeNode tr8 = new TreeNode(15);
		tr7.right=tr8;
		TreeNode tr9 = new TreeNode(-10);
		TreeNode tr10 = new TreeNode(3);
		tr4.left=tr9;
		tr4.right=tr10;
		
//		printTree(tr1);
		int[][] printTree = TreeSetPrint1.printTree(tr1);
		for (int i = 0; i < printTree.length; i++) {
				int[] js = printTree[i];
				System.out.println("--");
				for (int j = 0; j < js.length; j++) {
					int k = js[j];
					System.out.print(k+",");
				}
		}
		
	}

	private static void printTree(TreeNode tr1) {
		if (tr1==null) {
			return;
		}
		LinkedList<TreeNode> tempList=new LinkedList<>();
		tempList.add(tr1);
		while (!tempList.isEmpty()) {
			TreeNode poll = tempList.poll();
			System.out.println(poll.val+",");
	
			if (poll.left!=null) {
				tempList.add(poll.left);
			}
			if (poll.right!=null) {
				tempList.add(poll.right);
			}
			
		}
		
	}
}

class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;

	TreeNode(int x) {
		val = x;
	}
}

